package com.hzit.servlet.usre;

import com.hzit.bean.Users;
import com.hzit.dao.UsersDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.net.URLEncoder;

@WebServlet(name = "loginServlet", urlPatterns = "/user/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");


        //获取 用户名 和 密码
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //request获取session对象
        HttpSession session = request.getSession(); //如果有直接返回，如果没有创建一个session


        //调用数据库 完成用户登录业务
        //1.先通过用户名查询用户信息(用户不存在)
        UsersDao usersDao = new UsersDao();
        Users users = usersDao.findUsersByUsername(username);

        if (users != null) {


            if (users.getPwd().equals(password)) {


                //登录成功，存放数据到域中。需要使用，直接可以域中获取
                //登录成功跳转到主界面

                session.setMaxInactiveInterval(60*30);

                //user补齐  ip
                String remoteAddr = request.getRemoteAddr();

                session.setAttribute("userInfo", users);
                session.removeAttribute("loginErrorMsg"); //清楚错误信息

                String remember = request.getParameter("remember");

                if(remember!=null){
                    Cookie cookie = new Cookie("loginName", URLEncoder.encode(username,"UTF-8"));
                    cookie.setPath(request.getContextPath());
                    cookie.setMaxAge(60*60*24*7);

//                    Cookie cookie2 = new Cookie("loginName","xxx");
//                    cookie2.setPath(request.getContextPath());
//                    cookie2.setMaxAge(60*60*24*7);


                    response.addCookie(cookie);
                   // response.addCookie(cookie2);

                }else{
                    //如果原来有登录cookie,清空
                    Cookie[] cookies = request.getCookies();
                    if(cookies!=null){
                        for (Cookie cookie : cookies) {
                            String name = cookie.getName();
                            if ("loginName".equals(name)){

                                Cookie c1 = new Cookie("loginName","");
                                c1.setPath(request.getContextPath());

                                cookie.setMaxAge(0);
                                cookie.setValue("");
                                response.addCookie(c1);
                                break;
                            }
                        }
                    }
                }


                response.sendRedirect(request.getContextPath() + "/emp/all?page=1");


            } else {
                //登录失败?重新回到登录页面，并且携带错误信息
                //2.通过该用户名查询出来的密码和用户输入的密码进行比较(用户名或者密码错误)
                session.setAttribute("loginErrorMsg", "用户名或者密码错误!");
                response.sendRedirect(request.getContextPath() + "/jsp/Login.jsp");
            }

        } else {
            //登录失败?重新回到登录页面，并且携带错误信息
            session.setAttribute("loginErrorMsg", "用户名不存在!");
            response.sendRedirect(request.getContextPath() + "/jsp/Login.jsp");
        }

    }
}
